/**
 * 
 * Copyright (C) 2015 by Elcom Software Company
 *
 * @id: FBPostLikes.java, May 8, 2015
 *
 */
package com.nip.fblike.grapapi.api;

import org.json.JSONArray;
import org.json.JSONObject;

import com.nip.fblike.grapapi.GraphAPIBase;
import com.nip.fblike.grapapi.IFBLike;
import com.nip.fblike.graphobject.page.FBObjectLikeObject;
import com.nip.fblike.objects.UserLikes;

/**
 * @author ThangNM<thangnm@elcom.com.vn>
 *
 * @version v
 */
public class FBObjectLikes extends GraphAPIBase {

    /**
     * @param iFBLike
     */
    public FBObjectLikes(IFBLike iFBLike) {
        super(iFBLike);
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see com.nip.fblike.grapapi.IParserJson#parser(java.lang.String)
     */
    @Override
    public void parser(String json) {
        // TODO Auto-generated method stub
        FBObjectLikeObject object = new FBObjectLikeObject();
        object.setId(getUid());
        if(json == null || json.trim().isEmpty())
        {
            object.setEmpty(true);
            iFBLike.done(object);
            return;
        }
        
        try {
            JSONArray array = new JSONObject(json).getJSONArray("data");
            if(array == null || array.length() <= 0)
            {
                object.setEmpty(true);
                return;
            }
            UserLikes user ;
            for(int i = 0; i< array.length(); i++)
            {
                if(!array.isNull(i))
                {
                    user = new UserLikes();
                    user.setId(safeReadString(array.getJSONObject(i), "id"));
                    user.setName(safeReadString(array.getJSONObject(i), "name"));
                    object.addUser(user);
                }
            }
            
            
        } catch (Exception e) {
            // TODO: handle exception
            object.setEmpty(true);
        }finally
        {
            iFBLike.done(object);
        }
        
    }
    
    

    /* (non-Javadoc)
     * @see com.nip.fblike.grapapi.GraphAPIBase#getUrl()
     */
    @Override
    public String getUrl() {
        // TODO Auto-generated method stub
        return "https://graph.facebook.com/"+getUid()+"/likes?access_token="+getAccess_token()+"&limit=1000";
    }

}
